<!--
 * @Description:: 请补充填写
 * @Version: 1.0
 * @Author: 小白
 * @Date: 2025-07-25 10:54:36
 * @LastEditors: 小白
 * @LastEditTime: 2025-11-10 11:01:48
 * @FilePath: \tmsadmin\src\views\contract\contract\components\modalupload.vue
-->
<template>
  <el-dialog v-model="dialogVisible" title="导入员工信息" width="400px" :append-to-body="true" :before-close="handleClose">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" class="demo-ruleForm" status-icon>
      <el-form-item prop="fileList">
        <el-upload ref="uploadRef" class="upload-demo" :auto-upload="false" :limit="1" drag style="width: 100%;"
          :file-list="fileList" name="file" :headers="headers" :data="datas" :on-success="handleSuccess"
          @change="handleChange" :action="action">
          <el-icon class="el-icon--upload"><upload-filled /></el-icon>
          <div class="el-upload__text">
            点击或者拖拽上传的文件
          </div>
        </el-upload>
        <el-button type="text" size="mini" @click="downloadTemplate">
          下载模板
        </el-button>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCloses(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="submit(ruleFormRef)" v-loading="loading">
          确 认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { REQUEST } from '@/api/request'
const ruleFormRef = ref<FormInstance>()
import type { UploadInstance } from 'element-plus'
const uploadRef = ref<UploadInstance>()
const title = ref('')
const dialogVisible = ref(false)
const emits = defineEmits(['ok'])
const fileList = ref([])
import { getToken } from '@/plugins/cookie'
import { HTTP_URI } from '@/api/base_url'
import { store } from '@/store'
const action = ref(HTTP_URI.IMPORTSTAFF)
const headers = ref({
  token: getToken()
})
const datas = ref({
  houseId: store.state.userInfo.houseId,
})
const loading = ref(false)
const ruleForm = ref({
  fileList: []
})
const handleChange = (file: any, fileList: any) => {
  ruleForm.value.fileList = fileList
}
const rules = ref({
  fileList: [
    { required: true, message: '请选择导入的文件', trigger: 'change' }
  ]
})

const handleSuccess = (res: any) => {
  if (res.status == 200) {
    loading.value = false
    handleClose()
    emits('ok')
  } else {
    loading.value = false
    handleClose()
    ElMessage.warning(res.message)
  }
}
// 
const downloadTemplate = () => {
  // 下载文件并重新命名
  const a = document.createElement('a')
  a.href = 'https://img.kpsd56.com/20251020/template.xlsx'
  a.download = '员工信息导入模板.xlsx'
  a.click()
  a.remove()
}
const openModal = (t: any, e: any) => {
  fileList.value = []
  dialogVisible.value = true
}
const handleClose = () => {
  dialogVisible.value = false
  emits('ok')
}
const handleCloses = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emits('ok')
  dialogVisible.value = false
}
const submit = async (formEl: FormInstance | undefined) => {
  loading.value = true
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      uploadRef.value?.submit()


    }
  })
}
defineExpose({ openModal })
</script>

<style lang="" scoped></style>
